//
//  TrainingGoalSettingContainerController.swift
//  DaRings
//
//  Created by 尹琼 on 2022/12/1.
//

import UIKit
import JXSegmentedView
import MYModule

open class TrainingGoalSettingContainerController: ViewController {
    
    open var models: [TrainingGoalModel] = [] {
        
        didSet {
            reloadData()
        }
        
    }
    
    public var defaultSelectedIndex = 0
    
    public lazy var layout: UICollectionViewFlowLayout = {
        
        let layout = UICollectionViewFlowLayout()
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        
        layout.scrollDirection = .horizontal
        
        return layout
    }()
    
   
    public lazy var collectionView: CollectionView = {
        
        let collectionView = CollectionView(frame: CGRect.zero, collectionViewLayout: layout)
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        return collectionView
    }()

    open override func viewDidLoad() {
        super.viewDidLoad()
        
        
        collectionView.register(TrainingGoalSettingCell.self, forCellWithReuseIdentifier: TrainingGoalSettingCell.reuseIdentifier)
        
        collectionView.isPagingEnabled = true
        collectionView.dataSource = self
        
        view.addSubview(collectionView)
        
        collectionView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }

    }
    
    open override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        
       
        if !layout.itemSize.equalTo(collectionView.bounds.size) {
            layout.itemSize = collectionView.bounds.size
            collectionView.collectionViewLayout = layout
            collectionView.reloadData()
        }
        
    }
    
}


extension TrainingGoalSettingContainerController: UICollectionViewDataSource {
    

    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return models.count
        
    }
    
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: TrainingGoalSettingCell.reuseIdentifier, for: indexPath) as! TrainingGoalSettingCell
        
        cell.model = models[indexPath.item]
        
        return cell
        
    }
    
}


extension TrainingGoalSettingContainerController: JXSegmentedViewListContainer {
    
    
    public func contentScrollView() -> UIScrollView {
        return self.collectionView
    }
    
    public func reloadData() {
        
        collectionView.reloadData()
              
        
    }
    
    public func didClickSelectedItem(at index: Int) {
        
        let indexPath = IndexPath(item: index, section: 0)
        collectionView.scrollToItem(at: indexPath, at: .centeredHorizontally, animated: false)
        
    }
    
    
    
    
   
    
    
    
    
}
